package reign.engine;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import reign.engine.exceptions.CardIDTakenException;
import reign.engine.exceptions.InvalidCardXMLException;
import reign.engine.structures.Card;
import reign.engine.structures.Card.cardType;

public class CardBuilder {

	private static CardBuilder instance;
	
	private CardBuilder()
	{}
	
	public static void main(String [] args)
	{
		CardBuilder temp = CardBuilder.getInstance();
		String [] tempAry = {".\\config\\CreatureCards.xml"};
		temp.loadCardDirectory(tempAry);
		System.out.println(CardDirectory.getInstance().toString());
	}
	
	public static CardBuilder getInstance()
	{
		if(instance == null)
		{
			instance  = new CardBuilder();
		}
		return instance;
	}
	
	public void loadCardDirectory(String [] defaultConfigs)
	{
		for(int i = 0; i < defaultConfigs.length;i++)
		{
			 XMLReader readXML = new XMLReader(defaultConfigs[i]);
			 NodeList cards = readXML.getPeers(Constants.CARD_TAG);
			 Card[] returnMe = new Card[cards.getLength()];
			 CardDirectory main = CardDirectory.getInstance();
			 for(int j = 0; j < cards.getLength();j++)
			 {
				 try {
					main.addCard(parseCard(cards.item(j)));
				} catch (InvalidCardXMLException e) {
					System.err.println(e.toString());
				} catch (CardIDTakenException e) {
					System.err.println(e.toString());
				}
			 }
		}

	}

	public Card[] buildCards(String configFile)
	{
		 XMLReader readXML = new XMLReader(configFile);
		 NodeList cards = readXML.getPeers(Constants.CARD_TAG);
		 ArrayList<Card> returnMe = new ArrayList<Card>(cards.getLength());
		 for(int i = 0; i < cards.getLength();i++)
		 {
			 try {
				returnMe.add(parseCard(cards.item(i)));
			} catch (InvalidCardXMLException e) {
				System.err.println(e.toString());
			}
		 }
		 return returnMe.toArray(new Card[1]);
	}

	private Card parseCard(Node item) throws InvalidCardXMLException {
		String name;
		String description;
		String imageDir;
		int id;
		int toughness;
		int Strength;
		cardType type;
		try
		{
			name = item.getAttributes().getNamedItem("Name").getNodeValue();
			description = item.getAttributes().getNamedItem("Description").getNodeValue();
			imageDir = item.getAttributes().getNamedItem("Image").getNodeValue();
			id = Integer.parseInt(item.getAttributes().getNamedItem("ID").getNodeValue());
			Strength = Integer.parseInt(item.getAttributes().getNamedItem("Strength").getNodeValue());
			toughness = Integer.parseInt(item.getAttributes().getNamedItem("Toughness").getNodeValue());
			type = cardType.valueOf(item.getAttributes().getNamedItem("Type").getNodeValue().toUpperCase());
		}catch(Exception e)
		{
			throw new InvalidCardXMLException(item.getBaseURI(),getAttributes(item));
		};

	    Card returnMe = new Card(name,id,description,type,toughness,Strength,imageDir);
	    Node special = item.getFirstChild().getNextSibling();
	    if(special.getNodeName().equals(Constants.SPECIAL_ATTRIBUTES))
	    {
	    	NamedNodeMap list = special.getAttributes();
			for(int i = 0;i < Constants.MAX_ATTRIBUTES && i< list.getLength();i++)
			{
				returnMe.setProperty(list.item(i).getNodeName(),list.item(i).getNodeValue());
			}
	    }
		return returnMe;
		
		
	}
	
	public String getAttributes(Node item)
	{
		String data = "";
		for(int i = 0; i < item.getAttributes().getLength();i++)
		{
			data += item.getAttributes().item(i) + "\n\t";
		}
		return data;
	}
	
}

